package core;
import java.util.ArrayList;
import java.util.Timer;

import values.Bank;
import exception.ServerDownException;



/**
 * Class that has te list of current banks
 */
public class Banks 
{

	
	private static Timer timer;
	
	private static Banks_Task task;
	
	ArrayList<Bank> banks;
	
	
	//Constructor
	
	public Banks() throws ServerDownException
	{
		
		timer= new Timer();
		task= new Banks_Task();
		banks= Core.getBanks();
		timer.scheduleAtFixedRate(task, System.currentTimeMillis(), 1000*60);
	}
	
	public void setBanks(ArrayList<Bank> banks)
	{
		this.banks=banks;
	}
	
	public ArrayList<Bank> getBanks()
	{
		return banks;
	}
	
	//Hacer el metodo starts with 
	
	/**
	 * Method that given a string returns the most commented bank whose name starts with it. 
	 */
	public Bank getMostCommentedBank(String start)
	{
		int position=-1;
		
		int counter=0;
		
		for(int i=0;i<banks.size();i++)
		{
			Bank bank=banks.get(i);
			String name=bank.getName();
			int mentions=bank.getCounter();
			
			if(name.startsWith(start) && mentions>counter)
			{
				position=i;
				counter=mentions;
			}
		}
		if(position>-1)
		{
			return banks.get(position);
		}
		else
			return null;
	}
}
